package com.syh.jn.scm.service.impl.supplier.base.upstream;

import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.validator.ValidationUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.supplier.entity.base.upstream.UpstreamNewGoodsRequest;
import com.syh.jn.scm.domain.supplier.event.UpstreamNewGoodsRequestEvent;
import com.syh.jn.scm.domain.supplier.repository.base.upstream.UpstreamNewGoodsRequestRepository;
import com.syh.jn.scm.dto.supplier.base.upstreamNewGoodsRequest.UpstreamNewGoodsRequestMto;
import com.syh.jn.scm.dto.supplier.base.upstreamNewGoodsRequest.UpstreamNewGoodsRequestSto;
import com.syh.jn.scm.dto.supplier.base.upstreamNewGoodsRequest.UpstreamNewGoodsRequestVo;
import com.syh.jn.scm.service.api.supplier.base.upstream.UpstreamNewGoodsRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 上游商品申请
 */
@Service
@Transactional
public class UpstreamNewGoodsRequestServiceImpl extends AbstractServiceImpl<UpstreamNewGoodsRequestSto, UpstreamNewGoodsRequestMto, UpstreamNewGoodsRequestVo, UpstreamNewGoodsRequest, Long> implements UpstreamNewGoodsRequestService {
  @Autowired
  private UpstreamNewGoodsRequestRepository upstreamNewGoodsRequestRepository;

  @Override
  protected AggregateRepository<UpstreamNewGoodsRequest, Long> getAggregateRepository() {
    return upstreamNewGoodsRequestRepository;
  }

  /**
   * 更新商品审核信息
   *
   * @param mto
   */
  @Override
  public void update(UpstreamNewGoodsRequestMto mto) {
    ExtAssert.notNull(mto, "商品审核信息对象为空");
    ValidationUtils.validate(mto);

    UpstreamNewGoodsRequest upstreamNewGoodsRequest = upstreamNewGoodsRequestRepository.getById(mto.getId());
    ExtAssert.notNull(upstreamNewGoodsRequest, "商品审核信息对象为空");

    upstreamNewGoodsRequest.update(mto);
    upstreamNewGoodsRequestRepository.update(upstreamNewGoodsRequest);

    if (upstreamNewGoodsRequest.getAuditStatus().equals(Short.valueOf("1"))) {
      Object[] obj = new Object[]{upstreamNewGoodsRequest};
      //发送事件消息
      this.publicEvent(UpstreamNewGoodsRequestEvent.buildUpstreamNewGoodsRequestUpdate(obj));
    }
  }
}
